<?php 
/**     
 *
 * Copyright (c) 2009, Persistent Systems Limited
 *
 * Redistribution and use, with or without modification, are permitted 
 *  provided that the following  conditions are met:
 *   - Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *   - Neither the name of Persistent Systems Limited nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * Verb for Request To ACS
 */
define("REQUEST_TO_ACS", "Request To ACS");
  
/**
 * Verb for Response From ACS
 */
define("RESPONSE_FROM_ACS", "Response From ACS");

/**
 * Verb for Request To Service Bus
 */
define("REQUEST_TO_SERVICEBUS", "Request To Service Bus");

/**
 * Verb for Response From Service Bus
 */
define("RESPONSE_FROM_SERVICEBUS", "Response From Service Bus");

/**
 * Verb for Get ACS Token
 */
define("GET_ACS_TOKEN", "GetACSToken");

/**
 * Verb for Create Message Buffer
 */
define("CREATE_MSG_BUFFER", "CreateMessageBuffer");

/**
 * Verb for Get Message
 */
define("GET_MSG", "GetMessage");

/**
 * Constant fot Send Message 
 */
define("SEND_MSG", "SendMessage");

/**
 * Verb for Delete Message Buffer
 */
define("DELETE_MSG_BUFFER", "DeleteMessageBuffer");

/**
 * LoggerClass Class to write audit log 
 *
 */
class LoggerClass { 

   /**
	* @var boolean $_instance
	* @access private
	*/
    private static $_instance = 0; 
	/**
	* @var boolean $_fileName
	* @access private
	*/
    private static $_fileName;
	/**
	* @var boolean $_xmlFileHandle
	* @access private
	*/
	private static $_xmlFileHandle;
	/**
	* @var boolean $_timestamp
	* @access private
	*/
	private static $_timestamp;
	/**
	* Flag for turning audit log on or off. It's off by default.
	* @var boolean $doAuditLog
	* @access public
	*/
	public static $doAuditLog = false;
    /** 
     * LoggerClass constructor 
     * 
     * @access private 
     */ 
    private function __construct($fileName) { 
        
		LoggerClass::$_fileName = $fileName;
        if (!file_exists(LoggerClass::$_fileName)) { 
            LoggerClass::$_xmlFileHandle = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><logroot xmlns='http://www.w3.org/2005/Xml'/>");
			LoggerClass::$_xmlFileHandle->asXML(LoggerClass::$_fileName);
        }
    }

    public static function initAuditLogging($fileName) { 
        if (LoggerClass::$_instance == '0') { 
            LoggerClass::$_instance = new LoggerClass($fileName);
			LoggerClass::$doAuditLog = true;
        } 
        return LoggerClass::$_instance; 
    } 

	/** 
     * returns an instance of the LoggerClass object 
     * 
     * @access public 
     * 
     * @return an instance of the LoggerClass object 
     */ 
    public static function writeLogEntry($title, $rawData = null) { 
		LoggerClass::$_xmlFileHandle = simplexml_load_file(LoggerClass::$_fileName);
		$node = LoggerClass::$_xmlFileHandle->addChild('logentry');
		$node->addChild('timestamp', LoggerClass::getTimeStamp());
		$node->addChild('title', $title);
		if (!empty($rawData) && is_array($rawData)){
			$subnode = $node->addChild('rawdata');
			foreach($rawData  as $key=>$value){
				 $subnode->addChild($key, (string)$value);

			}
		} else {
			$node->addChild('rawdata', $rawData);
		}
		LoggerClass::$_xmlFileHandle->asXML(LoggerClass::$_fileName);				
		return true;
    } 
    /** 
     * Reads Xml file and return appropriate data
     * 
     * @access public 
     * 
     * @return an instance of the LoggerClass object 
     */ 
    public static function readLogEntry() { 
		$xmlDataArray  = array();
        if (file_exists(LoggerClass::$_fileName)) {
			$fileContents = file_get_contents(LoggerClass::$_fileName, true);
			if($fileContents !== ''){
				$xmlString = new SimpleXMLElement(LoggerClass::$_fileName,null,true);
				if($xmlString->getNamespaces()){
					foreach($xmlString as $xml) {
						$xmlDataArray[] = array('timestamp'=>(string)$xml->timestamp, 'title'=>(string)$xml->title, 'rawdata'=>htmlentities($xml->rawdata->asXML()));
					}
				}
				
			}			
		}
		return $xmlDataArray;		
    } 
	/** 
     * returns an current timestamp with microseconds
     * 
     * @access public 
     * 
     * @return an current timestamp with microseconds
     */ 
	public static function getTimeStamp()
	{
		list($usec, $sec) = explode(" ", microtime());
		LoggerClass::$_timestamp = ((float)$usec + (float)$sec);
		return LoggerClass::$_timestamp;
	}
} 
?> 